package com.livesource.client.web.mainMenu;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.livesource.client.LiveSourceGlobalVariables;
import com.livesource.client.utilities.LoadingPanel;
import com.livesource.client.web.liveSource.LiveSourcePage;
import com.livesource.client.web.liveTaskBoard.LiveTaskBoard;
import com.livesource.client.web.projectBacklog.ProjectBacklogPage;
import com.livesource.client.web.sourceCode.SourceCodePage;
import com.livesource.client.web.sourceCode.filesTree.FilesTree;
import com.livesource.client.web.sourceCode.filesTree.FilesViewList;
import com.livesource.client.webservices.project.RequestGetProject;

public class MenuAnchors extends Anchor {

	public MenuAnchors(String label, final String panelSelected,
			final String projectID) {

		this.setHTML(label);

		this.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				getPanel(panelSelected, projectID);
			}
		});
	}

	public static void getPanel(String panelSelected, String projectID) {

		if (LiveSourceMenu.MENU_LIVESOURCE.equals(panelSelected)) {

			LiveSourceGlobalVariables.showPanel.clear();
			LiveSourceGlobalVariables.showPanel.add(new LiveSourcePage());

		} else if (LiveSourceMenu.MENU_PROJECT_BACKLOG.equals(panelSelected)) {

			LiveSourceGlobalVariables.showPanel.clear();
			LiveSourceGlobalVariables.showPanel.add(new LoadingPanel());

		} else if (LiveSourceMenu.MENU_LIVE_TASK_BOARD.equals(panelSelected)) {

			LiveSourceMenu.popup.clear();

			LiveSourceMenu.popup.setWidget(new LiveTaskBoard());

			LiveSourceMenu.popup.show();

		} else if (LiveSourceMenu.MENU_SOURCE_CODE.equals(panelSelected)
				|| LiveSourceMenu.MENU_FEATURES.equals(panelSelected)) {

			LiveSourceMenu.popup.clear();

			LiveSourceMenu.popup.setWidget(new SourceCodePage(panelSelected));

			LiveSourceMenu.popup.show();
		}

		if (LiveSourceGlobalVariables.currentProject == null
				|| !projectID.equals(LiveSourceGlobalVariables.currentProject
						.getProjectID())) {

			RequestGetProject.getJsonpRequest(panelSelected, projectID);

		} else {

			if (LiveSourceMenu.MENU_PROJECT_BACKLOG.equals(panelSelected)) {

				LiveSourceGlobalVariables.showPanel.clear();
				LiveSourceGlobalVariables.showPanel
						.add(new ProjectBacklogPage());

			} else if (LiveSourceMenu.MENU_LIVE_TASK_BOARD
					.equals(panelSelected)) {

				LiveTaskBoard
						.showTasks(LiveSourceGlobalVariables.projectEntries);

			} else if (LiveSourceMenu.MENU_FEATURES.equals(panelSelected)) {

				FilesTree tree = new FilesTree(FilesViewList.VIEW_FEATURES,
						LiveSourceGlobalVariables.projectEntries);

				SourceCodePage.featuresTreePanel.clear();
				SourceCodePage.featuresTreePanel.add(tree);

			} else if (LiveSourceMenu.MENU_SOURCE_CODE.equals(panelSelected)) {

				FilesTree tree = new FilesTree(FilesViewList.VIEW_SOURCE_CODE,
						LiveSourceGlobalVariables.projectEntries);

				SourceCodePage.featuresTreePanel.clear();
				SourceCodePage.featuresTreePanel.add(tree);
			}
		}
	}
}
